//
//  HUD.m
//  YTXEducation
//
//  Created by 薛林 on 16/11/15.
//  Copyright © 2016年 YunTianXia. All rights reserved.
//

#import "HUD.h"
#import "AppDelegate.h"

int initCount;
static HUD *singleInstance;

@implementation HUD

- (void)showMessage:(NSString *)message tostalTime:(int)disTime{
    _mesgStr=message;
    _disTime=disTime;
    if (!isShow) {
        isShow=YES;
        UIFont *font=[UIFont systemFontOfSize:16];
        CGRect rect=[_mesgStr boundingRectWithSize:CGSizeMake(200, 100) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil];
        _lbmesg=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.height)];
        _lbmesg.font=font;
        _lbmesg.text=_mesgStr;
        _lbmesg.textAlignment = NSTextAlignmentCenter;
        _lbmesg.numberOfLines=0;
        _lbmesg.textColor=[UIColor whiteColor];
        _mesgView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width+20, rect.size.height+20)];
        _mesgView.center=CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
        _lbmesg.center=CGPointMake(_mesgView.bounds.size.width/2, _mesgView.bounds.size.height/2);
        _mesgView.backgroundColor=[UIColor blackColor];
        _mesgView.alpha=0.8;
        _mesgView.layer.cornerRadius=5.0;
        _mesgView.layer.masksToBounds=YES;
        [_mesgView addSubview:_lbmesg];
        AppDelegate *deledate=(AppDelegate*)[UIApplication sharedApplication] .delegate;
        [deledate.window addSubview:_mesgView];
        _timer=[NSTimer scheduledTimerWithTimeInterval:_disTime target:self selector:@selector(hiddenTostal) userInfo:nil repeats:NO];
        
    }else{
        initCount++;
        [_timer invalidate];
        _lbmesg.text=_mesgStr;
        UIFont *font=[UIFont systemFontOfSize:16];
        CGRect rect=[_mesgStr boundingRectWithSize:CGSizeMake(200, 100) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil];
        _mesgView.frame=CGRectMake(0, 0, rect.size.width+20, rect.size.height+20);
        _lbmesg.frame=CGRectMake(0, 0, rect.size.width, rect.size.height);
        _mesgView.center=CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
        _lbmesg.center=CGPointMake(_mesgView.bounds.size.width/2, _mesgView.bounds.size.height/2);
        _timer=[NSTimer scheduledTimerWithTimeInterval:_disTime target:self selector:@selector(hiddenTostal) userInfo:nil repeats:NO];
    }
}
- (void)hiddenTostal{
    isShow=NO;
    
    [_mesgView removeFromSuperview];
}
+(HUD *)shareHUD{
    @synchronized (self){
        if (singleInstance==nil) {
            singleInstance=[[HUD alloc] init];
        }
    }
    return singleInstance;
}


@end
